package com.studentexam.student.service.impl;

import com.studentexam.student.dao.LogMapper;
import com.studentexam.student.dao.PersonMapper;
import com.studentexam.student.dao.StudentMapper;
import com.studentexam.student.dao.TeacherMapper;
import com.studentexam.student.model.Person;
import com.studentexam.student.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class PersonServiceImpl implements PersonService {
    @Autowired
    private PersonMapper personMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private LogMapper logMapper;

    @Override
    public int selectPerson(String account, String password) {
        int selectPerson=personMapper.selectBy(account,password);
        if (selectPerson!=0){
            return 1;
        }else return -1;
    }

    @Override
    public int addPerson(Person person) {
        int returnInt=0;
//        1.数据判断
//        int rolenameCount=roleMapper.getRolenameCount(role.getRolename());
//        if (rolenameCount<1){
//        2.添加数据
//            int addRolenameInt=personMapper.insertPerson(person);
//        3.设置返回值
            returnInt=personMapper.insertPerson(person);
//        }else returnInt=-1;
        return returnInt;
    }

    @Override
    public int updatePerson(Person person) {
        int isUpdate=0;
        try{
            isUpdate=personMapper.updatePerson(person);
        }catch (Exception e){
            isUpdate=-1;
            System.out.println("更新人员信息时执行sql报错原因为:"+e.getMessage());
        }
        return isUpdate;
    }

    @Override
    public int delPerson(int personid) {
        int returnDel=0;
        try {
            if (studentMapper.getPersonCount(personid)<1&teacherMapper.getPersonCount(personid)<1&logMapper.getPersonCount(personid)<1) {
                returnDel = personMapper.delPerson(personid);
            }
        }catch (Exception e){
            returnDel=-1;
            System.out.println("删除失败原因为："+e.getMessage());
        }
        return returnDel;
    }

    @Override
    public List<Person> getByPersonid(int personid) {
        List<Person> personList=personMapper.getByPersonid(personid);
        return personList;
    }

    @Override
    public List<Person> getAllPerson(Person person) {
        List<Person> personList=personMapper.getAllPerson(person);
        return personList;
    }
}
